import { __filename, getFileName } from "../utils/tools.js";

// import.meta.url 是指当前模块的元信息
let fileName = __filename(import.meta.url);

// 获取短文件名
let shortFileName = getFileName(fileName);

// 数据库中的数据，这里暂时简化为一个数组
let films = [
    {
        id: 1,
        filmName: "与凤行",
        director: "邓科",
        actor: "赵丽颖 林更新",
        score: 9.9,
        flag: '仙侠 古偶'
    },
    {
        id: 2,
        filmName: "老友记",
        director: "大卫",
        actor: "很多人",
        score: 9.9,
        flag: '喜剧 情景'
    },
    {
        id: 3,
        filmName: "生活大爆炸",
        director: "小卫",
        actor: "谢耳顿 赖尔顿",
        score: 8.9,
        flag: '仙侠 古偶'
    },
    {
        id: 4,
        filmName: "家有儿女",
        director: "不知道谁",
        actor: "张一山 杨紫 宋丹丹 高亚麟",
        score: 9.9,
        flag: '喜剧 情景'
    },
    {
        id: 5,
        filmName: "编辑部的故事",
        director: "不知道",
        actor: "葛优 张国立 李力群",
        score: 9.9,
        flag: '情景'
    },
    {
        id: 6,
        filmName: "爱情公寓",
        director: "也不知道",
        actor: "陈赫 娄艺萧",
        score: 8.6,
        flag: '喜剧'
    },
    {
        id: 7,
        filmName: "AAA",
        director: "邓科",
        actor: "赵丽颖 林更新",
        score: 9.9,
        flag: '仙侠 古偶'
    },
    {
        id: 8,
        filmName: "BBBB",
        director: "邓科",
        actor: "赵丽颖 林更新",
        score: 9.9,
        flag: '仙侠 古偶'
    },
    {
        id: 9,
        filmName: "TTTT",
        director: "邓科",
        actor: "赵丽颖 林更新",
        score: 9.9,
        flag: '仙侠 古偶'
    },
    {
        id: 10,
        filmName: "UUUU",
        director: "邓科",
        actor: "赵丽颖 林更新",
        score: 9.9,
        flag: '仙侠 古偶'
    },
]

// http://localhost:3000/users
async function fn_getAll(ctx) {
    ctx.body = {
        code: 1000,
        data: films,
        msg: '获取所有电影成功'
    };
}
async function fn_getById(ctx) {
    let id = ctx.params.id * 1 || 0;
    console.log(id);
    let arr = films.filter(item => {
        return item.id === id;
    })
    console.log(arr);
    let res = {
        code: 1000,
        data: arr[0],
        msg: `获取指定id为${id}的电影成功`
    }
    ctx.body = res;
}
async function fn_post(ctx) {
    let obj = ctx.request.body;
    console.log(obj);
    //拿到模拟数据最后一个的id，然后加1就是为新电影的id
    let last = films[films.length - 1];
    console.log(last);
    let lastId = last.id + 1;
    obj['id'] = lastId;
    films.push(obj);
    ctx.body = obj;
}
async function fn_put(ctx) {
    let id = ctx.params.id * 1 || 0;
    let reqParm = ctx.request.body;
    let arr = films.filter(item => {
        return item.id === id;
    });
    if (arr.length > 0) {
        let obj = arr[0];
        obj.filmName = reqParm.filmName;
        obj.director = reqParm.director;
        obj.actor = reqParm.actor;
        obj.score = reqParm.score;
        obj.flag = reqParm.flag;

        ctx.body = {
            code: 1000,
            msg: '修改成功'
        }
    } else {
        ctx.body = {
            code: 4000,
            msg: `id为${id}的电影未找到，请确认后重试`
        }
    }
    // ctx.body = "修改电影";
}
async function fn_del(ctx) {
    let id = ctx.params.id * 1 || 0;
    let arr = films.filter(item => {
        return item.id === id;
    });
    let idx = films.findIndex(item => {
        return item.id === id;
    });
    // console.log(idx);
    if (arr.length > 0) {
        films.splice(idx, 1);
        console.log(films);
        ctx.body = {
            code: 1000,
            msg: `id为${id}的电影删除成功`
        }
    } else {
        ctx.body = {
            code: 4000,
            msg: `id为${id}的电影未找到，请确认后重试`
        }
    }
    // ctx.body = "删除电影";
}

let obj = {};

obj[`get /${shortFileName}`] = fn_getAll;
obj[`get /${shortFileName}/:id?`] = fn_getById;
obj[`post /${shortFileName}`] = fn_post;
obj[`put /${shortFileName}/:id?`] = fn_put;
obj[`del /${shortFileName}/:id?`] = fn_del;

export default obj;

